﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace wscf.lightblue.UI
{
	public partial class CodeGenerationDialog : Form
	{
		public CodeGenerationOptions Options { get; set; }

		public CodeGenerationDialog(CodeGenerationOptions options)
		{
			InitializeComponent();
			Options = options;
		}

		private void CodeGenerationDialog_Load(object sender, EventArgs e)
		{
			languageComboBox.Items.Clear();
			languageComboBox.Items.Add(ClrLanguage.cs.ToString());
			languageComboBox.Items.Add(ClrLanguage.vb.ToString());
			languageComboBox.Items.Add(ClrLanguage.cpp.ToString());
			ReferencedAssembliesListView.Columns.Add(new ColumnHeader()
			{
				Name = "Name"
			});
			EnableControls();
			LoadControls();
		}

		private void ReuseTypesInReferencedAssembliesCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			EnableControls();
		}

		private void EnableControls()
		{
			ReuseTypesInAllReferencedAssembliesRadioButton.Enabled = ReuseTypesInReferencedAssembliesCheckBox.Checked;
			ReuseTypesInSpecifiedReferencedAssembliesRadioButton.Enabled = ReuseTypesInReferencedAssembliesCheckBox.Checked;
			ReferencedAssembliesListView.Enabled = ReuseTypesInSpecifiedReferencedAssembliesRadioButton.Checked;
			if (dataContractsOnlyCheckBox.Checked)
			{
				createServerConfigurationCheckbox.Checked = false;
			}
			createServerConfigurationCheckbox.Enabled = !dataContractsOnlyCheckBox.Checked;
			configurationFileTextbox.Enabled = createServerConfigurationCheckbox.Checked;
			configurationFileButton.Enabled = createServerConfigurationCheckbox.Checked;
		}

		private void ReuseTypesInAllReferencedAssembliesRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			EnableControls();
		}

		private void ReuseTypesInSpecifiedReferencedAssembliesRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			EnableControls();
		}

		private void LoadControls()
		{
			metadataFilesTextBox.Text = Options.MetadataFiles;

			foreach (string item in languageComboBox.Items)
			{
				if (Options.Language.ToString() == item)
				{
					languageComboBox.SelectedItem = item;
				}
			}

			dataContractsOnlyCheckBox.Checked = Options.DataContractsOnly;
			dataContractsOnlyCheckBox.Enabled = !Options.DisableDataContractsOnly;

			dataContractSerializerCheckBox.Checked = Properties.Settings.Default.DataContractSerializer;

			if (Options.ReferencedAssemblies != null)
			{
				ReferencedAssembliesListView.Items.Clear();
				foreach (string path in Options.ReferencedAssemblies)
				{
					ReferencedAssembliesListView.Items.Add(new ListViewItem()
					{
						Text = Path.GetFileName(path),
						Tag = path
					});
					ReferencedAssembliesListView.Columns[0].Width = -1;
				}
			}

			destinationFileNameTextBox.Text = Options.OutputFile;

			destinationNamespaceTextBox.Text = Options.TargetNamespace;

			configurationFileTextbox.Text = Options.ConfigurationFile;
		}

		private void SaveControls()
		{
			Options.MetadataFiles = metadataFilesTextBox.Text;

			if(languageComboBox.SelectedItem != null)
			{
				if (languageComboBox.SelectedItem != null)
				{
					Options.Language = (ClrLanguage)Enum.Parse(typeof(ClrLanguage), languageComboBox.SelectedItem.ToString());
				}
			}

			Options.DataContractsOnly = dataContractsOnlyCheckBox.Checked;

			Options.DataContractSerializer = dataContractSerializerCheckBox.Checked;
			Properties.Settings.Default.DataContractSerializer = Options.DataContractSerializer;

			if (Options.ReferencedAssemblies == null)
			{
				Options.ReferencedAssemblies = new List<string>();
			}
			Options.ReferencedAssemblies.Clear();
			if (ReuseTypesInReferencedAssembliesCheckBox.Checked)
			{
				foreach (ListViewItem item in ReferencedAssembliesListView.Items)
				{
					if (ReuseTypesInAllReferencedAssembliesRadioButton.Checked || item.Checked)
					{
						Options.ReferencedAssemblies.Add(item.Tag.ToString());
					}
				}
			}

			Options.OutputFile = destinationFileNameTextBox.Text;

			Options.TargetNamespace = destinationNamespaceTextBox.Text;

			Options.ConfigurationFile = string.Empty;
			if (createServerConfigurationCheckbox.Checked)
			{
				Options.ConfigurationFile = configurationFileTextbox.Text;
			}
		}

		private void searchMetadataFilesButton_Click(object sender, EventArgs e)
		{
			if (metadatFilesOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				metadataFilesTextBox.Text = string.Join<string>(" ", metadatFilesOpenFileDialog.FileNames);
			}
		}

		private void generateButton_Click(object sender, EventArgs e)
		{
			SaveControls();
			Close();
		}

		private void cancelButton_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void searchAssembliesButton_Click(object sender, EventArgs e)
		{
			if (assembliesOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				foreach (string path in assembliesOpenFileDialog.FileNames)
				{
					ReferencedAssembliesListView.Items.Add(new ListViewItem()
					{
						Text = Path.GetFileName(path),
						Tag = path
					});
					ReferencedAssembliesListView.Columns[0].Width = -1;
				}
			}
		}

		private void configurationFileButton_Click(object sender, EventArgs e)
		{
			if (configurationFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				configurationFileTextbox.Text = configurationFileDialog.FileName;
			}
		}

		private void dataContractsOnlyCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			EnableControls();
		}

		private void createServerConfigurationCheckbox_CheckedChanged(object sender, EventArgs e)
		{
			EnableControls();
		}

	}
}
